﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SubTitleConvertors.Movies
{
    class SubDownload
    {
    }
}

/*
 * Please leave this Copyright notice in your code if you use it
 * Written by Decebal Mihailescu [http://www.codeproject.com/script/articles/list_articles.asp?userid=634640]
 */
namespace TextExtractors
{
    using System;
    using System.IO;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.ComponentModel.Composition;
    using CommonUtils;
    using GetMySubtitle;
    using System.Windows.Forms;
    using FreeTranslator;

    [Export(typeof(ITextImport))]
    [ExportMetadata("IsMovie", true)]
    [ExportMetadata("Description", "Movie files")]
    [ExportMetadata("FileExtension", "*.avi; *.mkv; *.mp4; *.divx; *.mov; *.mp4v; *.mpeg; *.ogv; *.wmv")]
    [ExportMetadata("ContextMenuText", "&Download Subtitle")]
    [ExportMetadata("IconName", @"internet.ico")]
    public class SubDownload : ITextImport
    {

        #region ITextImport Members

        public string GetAllTextFromFile(ref string fileName, out Encoding encoding)
        {
            if (string.IsNullOrEmpty(fileName))
            {
                throw new ArgumentException(null, "fileName");
            }
            if (!File.Exists(fileName))
            {
                throw new ArgumentException("File '" + fileName + "' is not found");
            }
            string str = string.Empty;
            encoding = null;
            try
            {
                var arg = new ProgressEventArgs();
                arg.Description = "Downloading Subtitle(s)...";
                arg.Progress = 0;
                if (ProgressChanged != null && ProgressChanged.GetInvocationList().Length > 0)
                    ProgressChanged(this, arg);
                //if (FreeTranslatorForm._mainForm is FreeTranslator.FreeTranslatorForm)
                //{
                GetMySubtitleForm dnldForm = Application.OpenForms.OfType<GetMySubtitleForm>().SingleOrDefault();
                if (dnldForm == null)
                {
                    dnldForm = new GetMySubtitleForm();
                }
                else
                {
                    //dnldForm.WindowState = FormWindowState.Normal;
                }
                dnldForm._txtLocation.Text = fileName;
                FreeTranslatorForm frm = FreeTranslatorForm._mainForm as FreeTranslator.FreeTranslatorForm;
                //dnldForm.Visible = false;
                Application.DoEvents();
                if (frm != null)
                {
                    dnldForm.ShowDialog(frm);
                }
                else
                {
                    dnldForm.Show();
                    Application.DoEvents();
                    dnldForm._buttonGetSubTitle_Click(null, null);
                }
                fileName = dnldForm._movieFile;
                if (File.Exists(dnldForm._movieFile))
                {

                    using (StreamReader reader = new StreamReader(dnldForm._movieFile, true))//GetEncoding(0x4e3)))
                    {
                        encoding = reader.CurrentEncoding;
                        return reader.ReadToEnd();
                    }
                }
                else
                    return "";
                //}
                //else
                //    return "";
            }
            catch (Exception exception)
            {
                throw new Exception("Error during text extraction from Text-file: " + fileName + Environment.NewLine + exception.Message);
            }
            //return str;
        }


        public event EventHandler ProgressChanged;
        #endregion
    }
}


